import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/components/gb_alert.dart';
import 'package:sinosheets/network/api.dart';
import 'package:sinosheets/network/interceptor/network_status_interceptor.dart';
import 'package:sinosheets/pages/brand_say/view.dart';
import 'package:sinosheets/pages/settings/view.dart';
import 'package:sinosheets/pages/shopping/view.dart';
import 'package:sinosheets/utils/bus_utils.dart';
import 'package:sinosheets/utils/common_utils/log_util.dart';
import 'package:sinosheets/utils/storage/LSUtil.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

class BaseTabLogic extends GetxController with GetSingleTickerProviderStateMixin {
  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: bottomBarPages.length, vsync: this);
  }

  @override
  void onReady() {
    super.onReady();
    listenNetworkStatus();
    Future.delayed(Duration(seconds: 1), () {
      showPromoCodeDialog();
    });
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  // final pageController = PageController(initialPage: 0);
  late TabController tabController;
  int currentIndex = 0;
  final List<Widget> bottomBarPages = [
    // HomePage(),
    // CategoryPage(),
    // FavoritePage(),
    // EstimatePage(),
    ShoppingPage(),
    BrandSayPage(),
    SettingsPage(),
  ];
  // int maxCount = 3;
  // final NotchBottomBarController controller = NotchBottomBarController(index: 0);

  ///监听网络状态
  void listenNetworkStatus() {
    ever(NetworkManager.instance.status, (value) {
      if (NetworkManager.instance.hasConnection) {
        bus.emit('network_refresh');
      } else {
        LogUtil.e('无网络');
      }
    });
  }

  ///显示弹框  设置promo code
  ///1. 如果设置了code，则不弹出
  ///2. 如果未设置code，则显示输入框
  ///3. 输入框输入code后，点击确定按钮，保存code
  ///4. 点击取消按钮，关闭弹框
  void showPromoCodeDialog() {
    ResourceApis.getDictData('sheetShowCode').then((value) {
      if (value.isSuccess) {
        List list = value.data;
        String sheetShowCode = 'false';

        ///遍历List 获取dictLabel是firstOrderAlert的map，取出dictValue的值
        for (var item in list) {
          if (item['dictLabel'] == 'sheetShowCode') {
            sheetShowCode = item['dictValue'];
            break;
          }
        }
        LSUtil.saveSheetShowCode(sheetShowCode);
        if (sheetShowCode == 'true') {
          if (LSUtil.getInviteCode()['code'].isNotEmpty) {
            return;
          }
          TextEditingController controller = TextEditingController();
          GBAlert.show(
              height: 190.r,
              title: 'Set Promo Code(Optional)',
              content: Column(
                children: [
                  TextField(
                    controller: controller,
                    decoration: InputDecoration(
                      hintText: 'Enter the Promo Code you obtained',
                      hintStyle: TextStyle(fontSize: 12.r, color: Colors.white),
                      border: OutlineInputBorder(
                        borderSide: BorderSide(color: ColorUtils.themeColor, width: 1.r),
                      ),
                    ),
                    style: TextStyle(fontSize: 14.r, color: Colors.white),
                  ),
                ],
              ),
              commitButtonText: 'Confirm',
              cancelButtonText: 'Cancel',
              commitCallBack: () {
                ResourceApis.validateInviteCode(controller.text).then((value) {
                  if (value.isSuccess) {
                    if (value.data == true) {
                      //保存code
                      LSUtil.saveInviteCode(controller.text, true);
                      bus.emit('code_update');
                      Get.back();
                    } else {
                      LSUtil.saveInviteCode(controller.text, false);
                      bus.emit('code_update');
                      Get.back();
                    }
                  }
                });
              });
        }
      }
    });
  }
}
